package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.UniversityDto;
import xyz.zhouzhaodong.crm.entity.University;
import xyz.zhouzhaodong.crm.mapper.UniversityMapper;
import xyz.zhouzhaodong.crm.service.IUniversityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 高校信息 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-22
 */
@Service
public class UniversityServiceImpl extends ServiceImpl<UniversityMapper, University> implements IUniversityService {

    @Resource
    UniversityMapper universityMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addUniversity(UniversityDto universityDto) {
        try{
            University university = universityDtoToUniversity(universityDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            university.setId(id);
            university.setCreateUser(universityDto.getStaffId());
            university.setCreateTime(new Date());
            universityMapper.insert(university);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteUniversity(UniversityDto universityDto) {
        try{
            University university = universityDtoToUniversity(universityDto);
            university.setUpdateUser(universityDto.getStaffId());
            university.setUpdateTime(new Date());
            // 删除
            university.setFlag(1);
            universityMapper.updateById(university);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateUniversity(UniversityDto universityDto) {
        try{
            University university = universityDtoToUniversity(universityDto);
            university.setUpdateUser(universityDto.getStaffId());
            university.setUpdateTime(new Date());
            university.setStaffId(null);
            universityMapper.updateById(university);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectUniversity(UniversityDto universityDto) {
        Integer flag = universityDto.getFlag() == null ? 0 : universityDto.getFlag();
        if(universityDto.getPageNo() != 0 && universityDto.getPageSize() != 0){
            // 分页查询
            Page<UniversityDto> page = new Page<>(universityDto.getPageNo(), universityDto.getPageSize());
            universityDto.setFlag(flag);
            IPage<UniversityDto> list = universityMapper.selectListPage(page, universityDto);
            return RestMessage.success(list);
        } else {
            QueryWrapper<University> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("staff_id", universityDto.getStaffId());
            queryWrapper.eq("flag", flag);
            // 高校名字
            if(universityDto.getUniversityName() != null && !"".equals(universityDto.getUniversityName())){
                queryWrapper.like("university_name", universityDto.getUniversityName());
            }
            // 学校类别
            if(universityDto.getSchoolType() != null && !"".equals(universityDto.getSchoolType())){
                queryWrapper.like("school_type", universityDto.getSchoolType());
            }
            // 城市
            if(universityDto.getCity() != null && !"".equals(universityDto.getCity())){
                queryWrapper.like("city", universityDto.getCity());
            }
            // 学校状态
            if(universityDto.getSchoolStatus() != null && !"".equals(universityDto.getSchoolStatus())){
                queryWrapper.eq("school_status", universityDto.getSchoolStatus());
            }
            List<University> list = universityMapper.selectList(queryWrapper);
            return RestMessage.success(list);
        }
    }

    @Override
    public RestMessage selectUniversityById(UniversityDto universityDto) {
        return RestMessage.success(universityMapper.selectById(universityDto.getId()));
    }

    @Override
    public RestMessage selectLinkUp(UniversityDto universityDto) {
        // 查询待沟通
        Integer flag = universityDto.getFlag() == null ? 0 : universityDto.getFlag();
        Page<UniversityDto> page = new Page<>(universityDto.getPageNo(), universityDto.getPageSize());
        universityDto.setFlag(flag);
        universityDto.setSchoolStatus("待沟通");
        IPage<UniversityDto> list = universityMapper.selectListPage(page, universityDto);
        return RestMessage.success(list);
    }

    /**
     * 实体类转化
     * @param universityDto
     * @return
     */
    private University universityDtoToUniversity(UniversityDto universityDto){

        University university = new University();
        university.setId(universityDto.getId());
        university.setStaffId(universityDto.getStaffId());
        university.setUniversityName(universityDto.getUniversityName());
        university.setSchoolType(universityDto.getSchoolType());
        university.setCity(universityDto.getCity());
        university.setSchoolStatus(universityDto.getSchoolStatus());
        university.setWebsite(universityDto.getWebsite());
        university.setMail(universityDto.getMail());
        university.setTelephone(universityDto.getTelephone());
        university.setTeacherTouch(universityDto.getTeacherTouch());
        university.setWeChat(universityDto.getWeChat());
        university.setRecruitStudentTrait(universityDto.getRecruitStudentTrait());
        university.setProvince(universityDto.getProvince());
        university.setCloseDate(universityDto.getCloseDate());
        university.setAdmissionSeason(universityDto.getAdmissionSeason());
        university.setConnectRecord(universityDto.getConnectRecord());
        university.setRemark(universityDto.getRemark());
        university.setFlag(universityDto.getFlag());
        return university;

    }

}
